package com.bishe.controller;

import cn.hutool.core.util.StrUtil;
import com.bishe.pojo.LoginModel;
import com.bishe.pojo.QueryModel;
import com.bishe.pojo.User;
import com.bishe.serivce.IUserService;
import com.bishe.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户控制层
 * @date 2022/03/03 47:03:07
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户控制器")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginModel loginModel){
        if (loginModel == null){
            throw new EcoBootException("用户名密码为空！");
        }
        if (StrUtil.isEmpty(loginModel.getUserName())||StrUtil.isEmpty(loginModel.getPassWord())){
            throw new EcoBootException("用户名或密码为空！");
        }

        LoginModel login = userService.login(loginModel);
        User one = new SqlQueryUtils<>(User.class, jdbcTemplate).eq(User::getUserName, login.getUserName()).getOne();
        login.setUser(one);
        return  Result.ok("ok",login);
    }




    @ApiOperation("添加和修改用户")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody User user,HttpServletRequest request){
        //检查admin登录
        JobUtil.checkAdminLogin(request,jdbcTemplate);
        if (user == null){
            throw new EcoBootException("user 为空！");
        }
        userService.addOrUpdate(user,request);

        return Result.ok();
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")String id){

        userService.deleteById(id);

        return Result.ok();
    }




    @ApiOperation("用户分页 昵称模糊")
    @GetMapping("/getPage")
    public Result getPage(QueryModel queryModel,HttpServletRequest request){
        //检查admin登录
        JobUtil.checkAdminLogin(request,jdbcTemplate);
        List<User> data = null;
        Integer pageNo = queryModel.getPageNo();
        Integer pageSize = queryModel.getPageSize();
        String searchTxt = queryModel.getSearchTxt();
        SqlQueryUtils<User> queryUtils = new SqlQueryUtils<User>(User.class, jdbcTemplate);

        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
        if (StrUtil.isNotEmpty(searchTxt)){
            queryUtils.like(User::getNikName,searchTxt);
        }

        //创建时间倒叙
        queryUtils.orderByDesc(User::getCreateTime);
        //都是0 不分页
        if (pageNo == 0 && pageSize == 0){
            data  =  queryUtils.getList();
        }else {
            data =  queryUtils.limit(pageNo,pageSize).getList();
        }

        return Result.ok(new PageResult<User>(queryModel,queryUtils,data));
    }
}
